﻿@using System.Net.Http.Json
@namespace MudBlazor.Docs.Examples
@inject HttpClient httpClient

<MudDataGrid Items="@_items" Groupable="true" Filterable="true">
    <Columns>
        <PropertyColumn Property="x => x.Name">
            <FooterTemplate>
                @{ int index = 1, count = @context.Items.Count(); }
                People: 
                @foreach (var person in @context.Items)
                {
                    <MudAvatar Color="@((Color)index)" Variant="Variant.Outlined" Size="Size.Small" Class="mr-1">@person.Name.First()</MudAvatar>@person.Name.Substring(1)
                    if (index < count)
                    {
                        @(", ")
                    }
                    index++;
                }
            </FooterTemplate>
        </PropertyColumn>
        <PropertyColumn Property="x => x.Age" AggregateDefinition="_ageAggregation" />
        <PropertyColumn Property="x => x.Status"></PropertyColumn>
        <PropertyColumn Property="x => x.Salary" AggregateDefinition="_salaryAggregation" />
        <PropertyColumn Property="x => x.HireDate" Title="Hired">
            <CellTemplate>
                @context.Item.HireDate.ToString("d")
            </CellTemplate>
        </PropertyColumn>
        <PropertyColumn Property="x => x.IsManager" Title="Manager" />
    </Columns>
</MudDataGrid>

@code { 
    public record Model (string Name, int Age, Severity Status, decimal Salary, DateTime HireDate, bool IsManager);

    IEnumerable<Model> _items = new List<Model>()
    {
        new Model("Sam", 56, Severity.Normal, 50_000.00M, new DateTime(2005, 3, 5), false), 
        new Model("Alicia", 54, Severity.Info, 75_000.00M, new DateTime(2010, 1, 17), false), 
        new Model("Ira", 27, Severity.Success, 102_000.00M, new DateTime(2017, 6, 15), true),
        new Model("John", 32, Severity.Warning, 132_000.00M, new DateTime(2021, 12, 23), true),
        new Model("Fred", 65, Severity.Warning, 87_000.00M, new DateTime(2003, 7, 3), false), 
        new Model("Tabitha", 33, Severity.Info, 157_000.00M, new DateTime(2015, 2, 12), true), 
        new Model("Hunter", 22, Severity.Success, 43_000.00M, new DateTime(2017, 9, 20), false),
        new Model("Esme", 55, Severity.Warning, 149_000.00M, new DateTime(2017, 8, 1), true)
    };

    AggregateDefinition<Model> _ageAggregation = new AggregateDefinition<Model>
    {
        Type = AggregateType.Avg,
        DisplayFormat = "Average age is {value}"
    };

    AggregateDefinition<Model> _salaryAggregation = new AggregateDefinition<Model>
    {
        Type = AggregateType.Custom,
        CustomAggregate = x =>
        {
            var highestSalary = x.Max(z => z.Salary);
            var countOver100Grand = x.Count(z => z.Salary > 100_000);
            return $"Highest: {highestSalary:C0} | {countOver100Grand} Over {100000:C0}";
        }
    };
}
